package com.example.behaviour.session;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class SessionMemoryImpl implements Session {

    private final ConcurrentHashMap<String,ChannelId> NAME_ID_MAP = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<ChannelId,String> ID_NAME_MAP = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<ChannelId, ChannelHandlerContext> ID_CTX_MAP = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<ChannelHandlerContext,ChannelId> CTX_ID_MAP = new ConcurrentHashMap<>();

    private final Map<String, Channel> usernameChannelMap = new ConcurrentHashMap<>();
    private final Map<Channel, String> channelUsernameMap = new ConcurrentHashMap<>();
    private final Map<Channel,Map<String,Object>> channelAttributesMap = new ConcurrentHashMap<>();

    @Override
    public void bindID_CTX(ChannelId channelId, ChannelHandlerContext ctx) {
        for(Map.Entry<String,ChannelId> entry : NAME_ID_MAP.entrySet())
        {
            if(entry.getValue().toString().equals(channelId.toString())){

            }
        }
        ID_CTX_MAP.put(channelId,ctx);
        CTX_ID_MAP.put(ctx,channelId);
    }

    @Override
    public void bindNAME_ID(String name, ChannelId channelId) {
        NAME_ID_MAP.put(name,channelId);
        ID_NAME_MAP.put(channelId,name);
    }

    @Override
    public void unbind(ChannelHandlerContext ctx) {
        ChannelId channelId = CTX_ID_MAP.remove(ctx);
        System.out.println("要解除绑定的channelId是 "+channelId.toString());
        ID_CTX_MAP.remove(channelId);
        String name = ID_NAME_MAP.remove(channelId);
        System.out.println("要解除绑定的name是 "+name);
        NAME_ID_MAP.remove(name);

        log.info("解除绑定 {}",this.toString());
    }

//    @Override
//    public void bind(Channel channel, String username) {
//        usernameChannelMap.put(username, channel);
//        channelUsernameMap.put(channel, username);
//        channelAttributesMap.put(channel, new ConcurrentHashMap<>());
//    }
//
//    @Override
//    public void unbind(Channel channel) {
//        String username = channelUsernameMap.remove(channel);
//        usernameChannelMap.remove(username);
//        channelAttributesMap.remove(channel);
//    }
//
//    @Override
//    public Object getAttribute(Channel channel, String name) {
//        return channelAttributesMap.get(channel).get(name);
//    }
//
//    @Override
//    public void setAttribute(Channel channel, String name, Object value) {
//        channelAttributesMap.get(channel).put(name, value);
//    }

    @Override
    public ChannelHandlerContext getChannel(String name) {
        System.out.println(NAME_ID_MAP.toString() + "---" + ID_CTX_MAP.toString());
//        return usernameChannelMap.get(username);
        System.out.println("mapName " + name);
        ChannelId channelId = null;
        try {
            channelId = NAME_ID_MAP.get(name);
        } catch (Exception e) {

        }
        if (channelId == null) {
            return null;
        }
        return ID_CTX_MAP.get(channelId);
    }

    @Override
    public ChannelHandlerContext getChannelById(ChannelId channelId) {
        System.out.println(NAME_ID_MAP.toString() + "---" + ID_CTX_MAP.toString());
//        return usernameChannelMap.get(username);
//        System.out.println("mapName " + name);
//        ChannelId channelId = null;
//        try {
//            channelId = NAME_ID_MAP.get(name);
//        } catch (Exception e) {
//
//        }
//        if (channelId == null) {
//            return null;
//        }
        return ID_CTX_MAP.get(channelId);
    }

    @Override
    public String toString() {
        return NAME_ID_MAP.toString() + "---" + ID_CTX_MAP.toString();
    }
}
